package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.PkGroup;
import reduce.fz.com.reduce.bean.Video;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.custom.TabIndicator;
import reduce.fz.com.reduce.fragment.FindTeamFragment;
import reduce.fz.com.reduce.fragment.FindVideoFragment;
import reduce.fz.com.reduce.fragment.FragmentCacheSupport;
import reduce.fz.com.reduce.fragment.PkVideoListFragment;
import reduce.fz.com.reduce.fragment.PkVideoRankFragment;
import reduce.fz.com.reduce.fragment.TeamCategoryFragment;
import reduce.fz.com.reduce.fragment.TeamMeDynamicListFragment;
import reduce.fz.com.reduce.util.QuPaiOper;
import reduce.fz.com.reduce.util.SetUtil;

public class PkVideoListActivity extends BaseToolBarActivity {
    //pk小组
    private PkGroup pkGroup;
    //pk小组视频列表
    private PkVideoListFragment fragment;
    //右上角btn
    private TextView rightBtn;
    //趣拍sdk
    private QuPaiOper quPaiOper;
    //视频
    private Video video;
    //挑战tab
    private List<String> pkTitles;
    //监听
    private TabIndicator.OnTabSelectedListener pkSelectListener;
    //当前展示fragment
    private Fragment mCurrentFragment;
    //fragment管理器
    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pk_video_list);
        mFragmentManager = getSupportFragmentManager();
        pkGroup = (PkGroup) getIntent().getSerializableExtra("pkgroup");
        rightBtn = setRightBtn(R.drawable.pk_video);
        quPaiOper = QuPaiOper.instance(this, false);
        initListener();
        setPkTab();
        attachFragment();
    }

    public void setPkTab() {
        if (pkTitles == null) {
            initPkTab();
        }
        setTabBar(pkTitles, pkSelectListener);
        showFragment(PkVideoListFragment.create(pkGroup), PkVideoListFragment.class.toString(), false);
        setTabBarPositon(0);

    }

    public Fragment getFragmentByContext(String name) {
        Fragment fragment = mFragmentManager.findFragmentByTag(name);
        return fragment;
    }

    public void showFragment(Fragment fragment, String cacheKey, Boolean showTitle) {
        android.support.v4.app.FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        //先确认如果和当前的fragment相同，则不做处理
        if (mCurrentFragment != null && mCurrentFragment == fragment) return;
        //从缓存中获取fragment
        Fragment cacheFragment = getFragmentByContext(cacheKey);
        if (mCurrentFragment != null)
            fragmentTransaction.detach(mCurrentFragment);
        if (cacheFragment != null && 1 != 1) {
            mCurrentFragment = cacheFragment;
            fragmentTransaction.attach(cacheFragment).commit();
        } else {
            mCurrentFragment = fragment;
            fragmentTransaction.add(R.id.container, fragment, cacheKey).commit();
        }
        if (showTitle)
            setToolbarTitle(((FragmentCacheSupport) fragment).getFragmentTitle());
    }

    public void initPkTab() {
        String[] temp = getResources().getStringArray(R.array.tab_pk_title);
        pkTitles = SetUtil.toList(temp);
        pkSelectListener = new TabIndicator.OnTabSelectedListener() {
            @Override
            public void onTabReselected(int position) {

            }

            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        showFragment(PkVideoListFragment.create(pkGroup), PkVideoListFragment.class.toString(), false);
                        break;
                    case 1:
                        showFragment(PkVideoRankFragment.create(pkGroup), PkVideoRankFragment.class.toString(), false);
                        break;
                    default:
                        break;
                }
            }
        };
    }

    /**
     * 录像
     */
    public void luxiang() {
        quPaiOper.showRecordPage();

    }

    @Override
    public void initWidget() {


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case QuPaiOper.RECORDE_SHOW:
                if (data == null) return;
                video = quPaiOper.dealIntent(data);
                forwardEdit(video);
                break;
            default:
                break;
        }
    }

    /**
     * 进入视频编辑页面
     *
     * @param video
     */
    public void forwardEdit(Video video) {
        VideoCard card = new VideoCard();
        card.video = video;
        Intent intent = new Intent(PkVideoListActivity.this, VideoEditDescActivity.class);
        Bundle bundle = new Bundle();
        card.pid = pkGroup.pid;
        bundle.putSerializable("videocard", card);
        intent.putExtras(bundle);
        startActivity(intent);


    }

    @Override
    public void initListener() {
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                luxiang();

            }
        });

    }

    public void attachFragment() {
        fragment = PkVideoListFragment.create(pkGroup);
        getSupportFragmentManager().beginTransaction().replace(R.id.main_container, fragment).commit();
    }
}
